<?php

function reequipment_userapi_getequipment($args)
{
    extract($args);

    // Optional arguments
    if (!isset($startnum)) {$startnum = 1;}
    if (!isset($numitems)) {$numitems = -1;}

    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['reequipment'];
    $ajoin = $xartable['reareas'];
    $mjoin = $xartable['remodels'];
    $rjoin = $xartable['rel_eq'];

    $query = "SELECT DISTINCT e.equipment_id, 
    				 e.model_id, 
    				 m.model_name, 
    				 e.area_id, 
    				 a.area_name, 
    				 e.company_id, 
    				 r.company_name, 
    				 e.serial_number, 
    				 e.machine_name,
    				 e.voltage,
    				 e.cycles,
    				 e.inactive"
    		. " FROM $table e"
    		
    		. " LEFT OUTER JOIN $ajoin a"
    		. " on e.area_id = a.area_id"
    		
    		. " LEFT OUTER JOIN $mjoin m"
    		. " on e.model_id = m.model_id"
    		
    		. " LEFT OUTER JOIN $rjoin r"
    		. " on e.company_id = r.company_id";    		
    		
    $where = array();
    $bind = array();

    if (!empty($model_id)) {
        $where[] = 'e.model_id = ?';
        $bind[] = (int)$model_id;
    }
    
    if (!empty($area_id)) {
        $where[] = 'e.area_id = ?';
        $bind[] = (int)$area_id;
    }

    if (!empty($equipment_id)) {
        $where[] = 'e.equipment_id = ?';
        $bind[] = (int)$equipment_id;
    }
    
    if (!empty($company_id)) {
        $where[] = 'e.company_id = ?';
        $bind[] = (int)$company_id;
    }    
    if (!empty($serial_number)) {
        $where[] = 'e.serial_number = ?';
        $bind[] = (string)$serial_number;
    }        
    if (!empty($machine_name)) {
        $where[] = 'e.machine_name = ?';
        $bind[] = (string)$machine_name;
    }    
    if (!empty($voltage)) {
        $where[] = 'e.voltage = ?';
        $bind[] = (string)$voltage;
    }    
    if (!empty($inactive)) {
        $where[] = 'e.inactive = ?';
        $bind[] = (int)$inactive;
    }    
    // Always assume we want active machines.
    if (empty($inactive)) {
        $where[] = 'e.inactive IS NULL';
    }    
    if (!empty($where)) {
        $query .= ' WHERE ' . implode(' AND ', $where);
    }

    // Not really necessary
    $query .= " ORDER BY e.area_id";
    
    // return $query;

    $result =& $dbconn->SelectLimit($query, $numitems, $startnum-1, $bind);
    if (!$result) return;

    $tags = array();
    for (; !$result->EOF; $result->MoveNext()) {
        list($equipment_id, 
        	 	$model_id, 
        	 	$model_name, 
        	 	$area_id, 
        	 	$area_name, 
        	 	$company_id, 
        	 	$company_name, 
        	 	$serial_number, 
        	 	$machine_name,
        	 	$voltage, 
        	 	$cycles,
        	 	$inactive) = $result->fields;

        $tags[] = array(
                'equipment_id'		=> $equipment_id,
                'model_id'     	 		=> $model_id,
                'model_name'		=> xarVarPrepForDisplay($model_name),
                'area_id'      			=> $area_id,
                'area_name'			=> xarVarPrepForDisplay($area_name),
                'company_id'    		=> $company_id,
                'company_name'	=> xarVarPrepForDisplay($company_name), 
                'serial_number'		=> xarVarPrepForDisplay($serial_number),    
                'machine_name'		=> xarVarPrepForDisplay($machine_name),    
                'voltage'				=> xarVarPrepForDisplay($voltage), 
                'cycles'    				=> $cycles,
                'inactive'    			=> $inactive
        );
    }

    $result->Close();
    return $tags;
}
?>